package com.golddaymicromall.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.golddaymicromall.activity.PruductDetialActivity;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.activity.salemanager.SaleManagerActivity;
import com.golddaymicromall.bean.ProductInfo;
import com.golddaymicromall.model.HttpModel;

import java.util.List;

/**
 * Created by 王立强 on 2016/10/6.
 */

public class ProductItemAdapter extends BaseAdapter {

    LayoutInflater layoutinflater;
    private Context ctx;
    private List<ProductInfo> list;

    public ProductItemAdapter(Context ctx, List<ProductInfo> list){
        this.ctx = ctx;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder hold;
        View view = convertView;
        if (view == null) {
            hold = new ViewHolder();
            layoutinflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutinflater.inflate(R.layout.item_productitem, null);
            hold.product_image = (ImageView) view.findViewById(R.id.product_image);
            hold.product_title = (TextView) view.findViewById(R.id.product_title);
            hold.product_price = (TextView) view.findViewById(R.id.product_price);
            hold.product_toPurchase = (Button) view.findViewById(R.id.product_toPurchase);
            hold.product_toDetail = (Button) view.findViewById(R.id.product_toDetail);
            view.setTag(hold);
        } else {
            hold = (ViewHolder) view.getTag();
        }
        Glide.with(ctx)
                .load(HttpModel.IMGURL + list.get(position).getImage())
                .placeholder(R.drawable.loading_4)
                .into(hold.product_image);
        hold.product_title.setText(list.get(position).getProductname());
        hold.product_price.setText("￥" + list.get(position).getPrice());
        hold.product_toPurchase.setOnClickListener(v -> {
            Intent toSaleMgt = new Intent(ctx, SaleManagerActivity.class);
            ctx.startActivity(toSaleMgt);
        });
        hold.product_toDetail.setOnClickListener(v -> {
            Intent toDetails = new Intent(ctx, PruductDetialActivity.class);
            toDetails.putExtra("id", list.get(position).getId());
            ctx.startActivity(toDetails);
        });
        return view;
    }

    public static class ViewHolder {
        ImageView product_image;
        TextView product_title;
        TextView product_price;
        Button product_toPurchase;
        Button product_toDetail;
    }
}
